﻿using System;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms; 
using Yanesdk.Draw;
using Yanesdk.System;
using ADV_Evaluator.Command;

using SD = System.Drawing;
using YD = Yanesdk.Draw;

namespace ADV_Evaluator {

    /// <summary>
    /// メッセージを表示するウィンドウ。
    /// 作成する際はYanesdkのScreen.SelectからUnselectの間で行う。
    /// </summary>
    public class Window_Message : Window_Base {

        private string text;
        private WindowAlign windowAlign;
        private StringFormat stringFormat;

        /// <summary>
        /// 表示するメッセージテキスト
        /// </summary>
        public string Text {
            get { return text; }
        }
        /// <summary>
        /// 画面上におけるウィンドウの揃え位置
        /// </summary>
        public WindowAlign WindowAlign {
            get { return windowAlign; }
        }
        /// <summary>
        /// ウィンドウ内の文章の揃え位置
        /// </summary>
        public StringFormat StringFormat {
            get { return stringFormat; }
            set { stringFormat = value; }
        }

        /// <summary>
        /// 直接大きさを指定するコンストラクタ。
        /// </summary>
        /// <param name="width">このウィンドウの幅</param>
        /// <param name="height">このウィンドウの高さ</param>
        public Window_Message(int width, int height) : base(width, height) {
            text = String.Empty;
            windowAlign = WindowAlign.Bottom;
            stringFormat = new StringFormat();
            stringFormat.LineAlignment = StringAlignment.Near;
            stringFormat.Alignment = StringAlignment.Near;
        }
        /// <summary>
        /// 画面の大きさからウィンドウの大きさを算出するコンストラクタ
        /// </summary>
        /// <param name="align">位置（上・中・下）</param>
        /// <param name="screenWidth">画面の幅</param>
        /// <param name="screenHeight">画面のの高さ</param>
        public Window_Message(WindowAlign align, int screenWidth, int screenHeight) : this(screenWidth, screenHeight / 3) {
            SetAlign(align, screenHeight);
        }

        /// <summary>
        /// 属性の値を全てセット
        /// </summary>
        /// <param name="param"></param>
        /// <param name="screenHeight">画面の高さ</param>
        public void SetParams(MessageOptionParams param, int screenHeight) {
            this.SetAlign(param.WindowAlign, screenHeight);
            this.StringFormat.Alignment = param.TextHorizontalAlign;
            this.StringFormat.LineAlignment = param.TextVerticalAlign;
            this.Visible = param.Visible;
            this.BorderVisible = param.BorderVisible;
            this.BorderColor = param.BorderColor;
            this.SkinColor1 = param.SkinColor1;
            this.SkinColor2 = param.SkinColor2;
            this.Font = new SD.Font(FontFamily.GenericSerif, param.FontSize);
            this.GradientMode = param.GradientMode;
        }

        // これ呼び出し禁止
        private new void SetParams(MessageOptionParams param) { }

        /// <summary>
        /// プロパティの値を属性の構造体にして返す
        /// </summary>
        /// <returns></returns>
        public override MessageOptionParams GetParams() {
            MessageOptionParams param = new MessageOptionParams();
            param.WindowAlign = this.WindowAlign;
            param.TextHorizontalAlign = this.StringFormat.Alignment;
            param.TextVerticalAlign = this.StringFormat.LineAlignment;
            param.Visible = this.Visible;
            param.BorderVisible = this.BorderVisible;
            param.BorderColor = this.BorderColor;
            param.SkinColor1 = this.SkinColor1;
            param.SkinColor2 = this.SkinColor2;
            param.FontSize = this.Font.Size;
            param.GradientMode = this.GradientMode;
            return param;
        }


        /// <summary>
        /// ウィンドウの位置を設定
        /// </summary>
        /// <param name="align">位置</param>
        /// <param name="screenHeight">スクリーンの高さ</param>
        public void SetAlign(WindowAlign align, int screenHeight) {
            this.windowAlign = align;
            switch (align) {
            case WindowAlign.Top:
                y = 0;
                break;
            case WindowAlign.Middle:
                y = (screenHeight / 3) * 1;
                break;
            case WindowAlign.Bottom:
                y = (screenHeight / 3) * 2;
                break;
            }
        }

        /// <summary>
        /// テキストを指定し、表示する。
        /// </summary>
        /// <param name="text"></param>
        public void Show(string text) {
            this.text = text;
            Refresh();
        }

        /// <summary>
        /// 再描画
        /// </summary>
        public override void Refresh() {
            RefreshWithoutTexture();
            texture.SetBitmap(bitmap);
        }

        /// <summary>
        /// YanesdkのGlTextureを伴わない再描画。ビットマップだけで使いたいときはこちらで。
        /// </summary>
        public void RefreshWithoutTexture() {
            DrawBackground();
            DrawText();
        }

        /// <summary>
        /// テキストの描画
        /// </summary>
        private void DrawText() {
            //using (GraphicsPath gp = new GraphicsPath())
            using (Graphics graphics = Graphics.FromImage(bitmap)) {
                // アンチエイリアスをかける
                graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
                //graphics.SmoothingMode = SmoothingMode.AntiAlias;
                //graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                // 文字列を縁取りして描画
                //gp.AddString(text, font.FontFamily, (int)font.Style, font.Size, contentRect, stringFormat);
                //graphics.FillPath(textBrush, gp);
                //graphics.DrawPath(Pens.Black, gp);
                graphics.DrawString(text, font, textBrush, contentRect, stringFormat);
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="system"></param>
        public override void Update(GameSystem system) {
            base.Update(system);
        }

        /// <summary>
        /// 解放
        /// </summary>
        public override void Dispose() {
            base.Dispose();
        }

    }


}
